﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spider.Visited;

namespace Spider
{
    /// <summary>
    /// 待访问的Url队列
    /// </summary>
    internal static class ToDo
    {
        private static Queue<ToDoData> _queue = new Queue<ToDoData>();
        /// <summary>
        /// 队列长度
        /// </summary>
        public static int Length
        {
            get { return _queue.Count; }
        }
        /// <summary>
        /// 将不在已访问列表中的Url添加到访问队列
        /// </summary>
        /// <param name="urldata"></param>
        public static void AddNotExistVisitedUrl(ToDoData urldata)
        {
            DicVisited visited=new DicVisited();
            if (!visited.ContainUrl(urldata.PageUrl))
                _queue.Enqueue(urldata);
        }
        /// <summary>
        /// 将Url添加到队列
        /// </summary>
        /// <param name="urldata"></param>
        public static void AddUrl(ToDoData urldata)
        {
            _queue.Enqueue(urldata);
        }
        /// <summary>
        /// 从队列中取出一个Url
        /// </summary>
        /// <returns></returns>
        public static ToDoData GetUrl()
        {
            ToDoData urldata = null;
            if (_queue.Count != 0)
            {
                try
                {
                    urldata = _queue.Dequeue();
                }
                catch (Exception)
                {

                }
            }

            return urldata;
        }
        /// <summary>
        /// 获取整个队列
        /// </summary>
        /// <returns></returns>
        public static Queue<ToDoData> GetAll()
        {
            return _queue;
        } 
    }
}
